home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / libs / vopl / glvopl.lha / glvopl / src / switches.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-07  |  7.5 KB  |  463 lines

  1. #include <stdio.h>
  2. #include <math.h>
  3. #ifdef SGI_GL
  4. #include "gl.h"
  5. #else
  6. #include "vogl.h"
  7. #endif
  8.  
  9. #include "vopl.h"
  10.  
  11. static    char    charbuf[10];
  12.  
  13. float    cardinal[4][4] = {
  14.     {-0.5,    1.5,    -1.5,    0.5},
  15.     {1.0,    -2.5,    2.0,    -0.5},
  16.     {-0.5,    0.0,    0.5,    0.0},
  17.     {0.0,    1.0,    0.0,    0.0}
  18. };
  19.  
  20. /*
  21.  * the initial structure
  22.  */
  23. vopldev plotdev = {
  24.     0.0, 0.0,        /* s1, sn */
  25.     0.75,            /* markerscale */
  26.     STRAIGHT_LINE,        /* fit */
  27.     2,            /* degree */
  28.     0,            /* spline type */
  29.     0,            /* grid? */
  30.     0, 1,            /* startind, arrayind */
  31.     256, 0, 0,        /* precision, markerspacing, forceticks */
  32.     (char *)0,        /* marker */
  33.     (char *)0,        /* graphtitle */
  34.     {                        /* x axis */
  35.         {
  36.             BLOODYBIG,    /* min */
  37.             -BLOODYBIG,     /* max */
  38.             1.0,        /* div */
  39.             LINEAR,        /* scaling */
  40.             1,        /* annotate? */
  41.             5,        /* nticks */
  42.             1,        /* ntspacing */
  43.             10,        /* minorticks */
  44.             0,        /* scaleset */
  45.             (char *)0,    /* format */
  46.             (char *)0    /* title */
  47.         },
  48.         {                        /* y axis */
  49.             BLOODYBIG,    /* min */
  50.             -BLOODYBIG,     /* max */
  51.             1.0,        /* div */
  52.             LINEAR,        /* scaling */
  53.             1,        /* annotate? */
  54.             5,        /* nticks */
  55.             1,        /* ntspacing */
  56.             10,        /* minorticks */
  57.             0,        /* scaleset */
  58.             (char *)0,    /* format */
  59.             (char *)0    /* title */
  60.         },
  61.         {                        /* z axis */
  62.             BLOODYBIG,    /* min */
  63.             -BLOODYBIG,     /* max */
  64.             1.0,        /* div */
  65.             LINEAR,        /* scaling */
  66.             1,        /* annotate? */
  67.             5,        /* nticks */
  68.             1,        /* ntspacing */
  69.             10,        /* minorticks */
  70.             0,        /* scaleset */
  71.             (char *)0,    /* format */
  72.             (char *)0    /* title */
  73.         }
  74.     }
  75. };
  76.  
  77. /*
  78.  * fit
  79.  *
  80.  *    Specify the type of fit for a graph
  81.  */
  82. void
  83. fit(type)
  84.     int    type;
  85. {
  86.     char    err_buf[EBUF_SIZE];
  87.  
  88.     if (type < 0 || type > MAX_FIT) {
  89.         sprintf(err_buf, "fit: unknown fit type: %d", fit);
  90.         vopl_error(err_buf);
  91.     }
  92.  
  93.     plotdev.fit = type;
  94. }
  95.  
  96. /*
  97.  * skip
  98.  *
  99.  *    causes scaling and plotting to skip every n data points
  100.  */
  101. skip(n)
  102.     int    n;
  103. {
  104.     plotdev.arrayind = n;
  105. }
  106.  
  107. /*
  108.  * endslopes
  109.  *
  110.  *    Specify the end slopes for a cubic spline fit.
  111.  *    If No slopes have been specified, then we'll use
  112.  *    the VOGLE curve routine.
  113.  */
  114. void
  115. endslopes(a, b)
  116.     float    a, b;
  117. {
  118.     plotdev.s1 = a;
  119.     plotdev.sn = b;
  120.     plotdev.splinetype = CLAMPED;
  121. }
  122.  
  123. /*
  124.  * gridspacing
  125.  *
  126.  *    Turns grids on or off
  127.  */
  128. void
  129. gridspacing(spacing)
  130.     int    spacing;
  131. {
  132.     plotdev.grid = spacing;
  133. }
  134.  
  135. /*
  136.  * scaling
  137.  *
  138.  *    Specify the type of scaling to be used for axis drawing
  139.  * and graph plotting.
  140.  */
  141. void
  142. scaling(type, axis)
  143.     int    type;
  144.     char    axis;
  145. {
  146.     char    err_buf[EBUF_SIZE];
  147.  
  148.     switch (axis) {
  149.     case 'x':
  150.     case 'X':
  151.         plotdev.axes[XIND].scaling = type;
  152.         break;
  153.     case 'y':
  154.     case 'Y':
  155.         plotdev.axes[YIND].scaling = type;
  156.         break;
  157.     case 'z':
  158.     case 'Z':
  159.         plotdev.axes[ZIND].scaling = type;
  160.         break;
  161.     default:
  162.         sprintf(err_buf, "scaling: unknown axis '%c'", axis);
  163.         vopl_error(err_buf);
  164.     }
  165. }
  166.  
  167. /*
  168.  * tickmarks
  169.  *
  170.  *    Specify the number of tickmarks on an axis. For autoscaled
  171.  *    axes (ie those using "adjustscale"), the number of tick marks
  172.  *    may not come out exactly as specified.
  173.  */
  174. void
  175. tickmarks(num, axis)
  176.     int    num;
  177.     char    axis;
  178. {
  179.     char    err_buf[EBUF_SIZE];
  180.  
  181.     if (num < 0) {
  182.         sprintf(err_buf,"tickmarks: number of tickmarks on axis '%c' must be >= 0 - was %d", axis, num);
  183.         vopl_error(err_buf);
  184.     } else {
  185.         switch (axis) {
  186.         case 'x':
  187.         case 'X':
  188.             plotdev.axes[XIND].nticks = num;
  189.             break;
  190.         case 'y':
  191.         case 'Y':
  192.             plotdev.axes[YIND].nticks = num;
  193.             break;
  194.         case 'z':
  195.         case 'Z':
  196.             plotdev.axes[ZIND].nticks = num;
  197.             break;
  198.         default:
  199.             sprintf(err_buf,"tickmarks: unknown axis '%c'", axis);
  200.             vopl_error(err_buf);
  201.         }
  202.  
  203.         plotdev.forceticks = 1;
  204.     }
  205. }
  206.  
  207. /*
  208.  * tickspacing
  209.  *
  210.  *    Specify the spacing of tickmarks on an axis. For autoscaled
  211.  *    axes (ie those using "adjustscale"), the number of tick marks
  212.  *    may not come out exactly as specified.
  213.  */
  214. void
  215. tickspacing(num, axis)
  216.     int    num;
  217.     char    axis;
  218. {
  219.     char    err_buf[EBUF_SIZE];
  220.  
  221.     if (num < 0) {
  222.         sprintf(err_buf,"tickmarks: number of tickmarks on axis '%c' must be >= 0 - was %d", axis, num);
  223.         vopl_error(err_buf);
  224.     } else {
  225.         switch (axis) {
  226.         case 'x':
  227.         case 'X':
  228.             plotdev.axes[XIND].ntspacing = num;
  229.             break;
  230.         case 'y':
  231.         case 'Y':
  232.             plotdev.axes[YIND].ntspacing = num;
  233.             break;
  234.         case 'z':
  235.         case 'Z':
  236.             plotdev.axes[ZIND].ntspacing = num;
  237.             break;
  238.         default:
  239.             sprintf(err_buf, "tickspacing: unknown axis '%c'", axis);
  240.             vopl_error(err_buf);
  241.         }
  242.     }
  243. }
  244.  
  245. /*
  246.  * minorticks
  247.  *
  248.  *    Specify the number of minortickmarks bewteen each tick on an axis. 
  249.  *     On a log scaled axis, this simply says to draw/not draw the minor
  250.  *    minor tick marks ... their number being always set to 10.
  251.  */
  252. void
  253. minorticks(num, axis)
  254.     int    num;
  255.     char    axis;
  256. {
  257.     char    err_buf[EBUF_SIZE];
  258.  
  259.     if (num < 0) {
  260.         sprintf(err_buf,"minorticks: number of minor tickmarks on axis '%c' must be >= 0 - was %d", axis, num);
  261.         vopl_error(err_buf);
  262.     } else {
  263.         switch (axis) {
  264.         case 'x':
  265.         case 'X':
  266.             plotdev.axes[XIND].minorticks = num;
  267.             break;
  268.         case 'y':
  269.         case 'Y':
  270.             plotdev.axes[YIND].minorticks = num;
  271.             break;
  272.         case 'z':
  273.         case 'Z':
  274.             plotdev.axes[ZIND].minorticks = num;
  275.             break;
  276.         default:
  277.             sprintf(err_buf,"minorticks: unknown axis '%c'", axis);
  278.             vopl_error(err_buf);
  279.         }
  280.     }
  281. }
  282.  
  283. /*
  284.  * annotate
  285.  *
  286.  *    Turn on or off labeling of the drawn axes
  287.  */
  288. void
  289. annotate(format, axis)
  290.     char        *format, axis;
  291. {
  292.     axisdata    *ax;
  293.     char        err_buf[EBUF_SIZE];
  294.  
  295.  
  296.     /*
  297.      * wooh Jimmy!
  298.      */
  299.     switch (axis) {
  300.     case 'x':
  301.     case 'X':
  302.         ax = &plotdev.axes[XIND];
  303.         break;
  304.     case 'y':
  305.     case 'Y':
  306.         ax = &plotdev.axes[YIND];
  307.         break;
  308.     case 'z':
  309.     case 'Z':
  310.         ax = &plotdev.axes[ZIND];
  311.         break;
  312.     default:
  313.         sprintf(err_buf, "annotate: unknown axis '%c'", axis);
  314.         vopl_error(err_buf);
  315.     }
  316.  
  317.     ax->annotate = (format ? 1 : 0);
  318.     ax->format = savestr(ax->format, format);
  319. }
  320.  
  321. /*
  322.  * arrayindex
  323.  *
  324.  *    How to index into the arrays passed to adjustscale and plot
  325.  *    eg, increment the index by one or by more.
  326.  *
  327.  */
  328. void
  329. arrayindex(i)
  330.     int    i;
  331. {
  332.     plotdev.arrayind = i;
  333. }
  334.  
  335. /*
  336.  * marker
  337.  *
  338.  *    Defines the current marker string to be used on subsequent
  339.  *    plot calls.
  340.  */
  341. void
  342. marker(string)
  343.     char    *string;
  344. {
  345.     plotdev.marker = savestr(plotdev.marker, string);
  346. }
  347.         
  348. /*
  349.  * markerspacing
  350.  *
  351.  *    Sets the spacing for the markers.
  352.  */
  353. void
  354. markerspacing(spacing)
  355.     int    spacing;
  356. {
  357.     plotdev.markerspacing = spacing;
  358. }
  359.  
  360. /*
  361.  * markerscale
  362.  *
  363.  *    Defines a multiplicative scale factor (from the default) for
  364.  *    the size of markers drawn.
  365.  */
  366. void
  367. markerscale(s)
  368.     float    s;
  369. {
  370.     plotdev.markerscale = s;
  371. }
  372.  
  373.  
  374. /*
  375.  * graphtitle
  376.  *
  377.  *    sets the title for the graph
  378.  */
  379. void
  380. graphtitle(s)
  381.     char    *s;
  382. {
  383.     plotdev.graphtitle = savestr(plotdev.graphtitle, s);
  384. }
  385.         
  386.  
  387. /*
  388.  * drawtitle
  389.  *
  390.  *     Draws a title for the graph.
  391.  */
  392. void
  393. drawtitle()
  394. {
  395.     if (plotdev.graphtitle == (char *)NULL)
  396.         return;
  397.  
  398.     pushattributes();
  399.     pushmatrix();
  400.     pushviewport();
  401.  
  402.     ortho2(-1.0, 1.0, -1.0, 1.0);
  403.  
  404.     htextsize(TEXTWIDTH * 1.5, TEXTHEIGHT * 1.5);
  405.  
  406.     move2(0.5 * (XMAX - XMIN) + XMIN, YMAX + 2 * TEXTHEIGHT);
  407.     hcentertext(1);
  408.  
  409.     hcharstr(plotdev.graphtitle);
  410.  
  411.     hcentertext(0);
  412.     popviewport();
  413.     popmatrix();
  414.     popattributes();
  415. }
  416.  
  417.  
  418. /*
  419.  * range
  420.  *
  421.  *    explicitly set the range of an axis
  422.  */
  423. void
  424. range(min, max, axis)
  425.     float    min, max;
  426.     char    axis;
  427. {
  428.     axisdata    *ax;
  429.     char        err_buf[EBUF_SIZE];
  430.  
  431.  
  432.     switch (axis) {
  433.     case 'x':
  434.     case 'X':
  435.         ax = &plotdev.axes[XIND];
  436.         ax->div = (WhatX(max) - WhatX(min)) / ax->nticks;
  437.         break;
  438.     case 'y':
  439.     case 'Y':
  440.         ax = &plotdev.axes[YIND];
  441.         ax->div = (WhatY(max) - WhatY(min)) / ax->nticks;
  442.         break;
  443.     case 'z':
  444.     case 'Z':
  445.         ax = &plotdev.axes[ZIND];
  446.         ax->div = (WhatZ(max) - WhatZ(min)) / ax->nticks;
  447.         break;
  448.     default:
  449.         sprintf(err_buf, "range: unknown axis '%c'", axis);
  450.         vopl_error(err_buf);
  451.     }
  452.  
  453.     ax->min = min;
  454.     ax->max = max;
  455.  
  456.     if (ax->scaling == LINEAR)
  457.         ax->div = (max - min) / ax->nticks;
  458.     else
  459.         ax->div = (log10((double)max) - log10((double)min)) / ax->nticks;
  460.  
  461.     ax->scaleset = 1;
  462. }
  463.